﻿using System;
using System.Collections.Generic;
using System.Text;
using OpenGLES;
using System.IO;

namespace PFD
{
    public class RenderTextureOGL : IRenderTexture
    {
        /// <summary>
        /// Store the OpenGL Texture Resource name
        /// </summary>
        OpenGLES.Texture tName;

        /// <summary>
        /// Maintain an internal flag to indicate whether or not the object has already been disposed
        /// </summary>
        bool Disposed = false;

        #region IRenderTexture Members

        /// <summary>
        /// Provide a property to access the Name of the Texture Resource
        /// This is an OpenGL specific property
        /// </summary>
        public uint Name { get { return tName.Name; } }

        #endregion

        /// <summary>
        /// Public contructor. Creates a new OpenGL Texture object from stream s.
        /// The newly created texture will be created transparent or opaque according
        /// to the flag provided in bIsTransparent.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="bIsTransparent"></param>
        public RenderTextureOGL(Stream s, bool bIsTransparent)
        {
            //  Simply create the new texture and store it in tName
            tName = OpenGLES.Texture.LoadStream(s, bIsTransparent);
        }

        #region IDisposable Members

        /// <summary>
        /// Dispose the object and related resources
        /// </summary>
        unsafe public void Dispose()
        {
            if (!Disposed)
            {
                uint name = tName.Name;
                OpenGLES.gl.DeleteTextures(0, &name);
                Disposed = true;
            }
        }

        #endregion
    }
}
